﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class MenuMaintainController : Controller
    {
        private MenuMaintainInfo meninfo = new MenuMaintainInfo(true);
        public ActionResult MenuMaintainIndex()
        {
            ViewData["MenuType"] = GetMenuType();
            return View();
        }

        /// <summary>
        /// 获取菜单类型列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMenuType()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "请选择...");
            dic.Add(1, "大荤");
            dic.Add(2, "小荤");
            dic.Add(3, "素菜");
            dic.Add(4, "汤饭点心类");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取菜单类型列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMenuTypeAdd()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "大荤");
            dic.Add(2, "小荤");
            dic.Add(3, "素菜");
            dic.Add(4, "汤饭点心类");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string query = " where 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                if (param[0].ToString() != "-1")
                {
                    query += " and MenuType='" + param[0] + "'";
                }
                if (param[1].ToString() != "")
                {
                    query += " and MenuName like '%" + param[1] + "%'";
                }
            }
            IList<MenuMaintainInfo> menlist = meninfo.SelMenuInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = meninfo.GetMenucount(query);
            List<string> v;
            foreach (MenuMaintainInfo item in menlist)
            {
                #region
                v = new List<string>();
                v.Add(item.MenuId.ToString());
                v.Add(item.MenuName);
                v.Add(item.MenuTypeName.ToString());
                v.Add(item.MenuPrice.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.MenuId.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        public ActionResult AddMenu()
        {
            ViewData["MenuType"] = GetMenuTypeAdd();
            return View();
        }

        /// <summary>
        /// 保存菜单（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveMenuAdd(MenuMaintainVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                MenuMaintainInfo man = new MenuMaintainInfo()
                {
                    MenuName = model.MenuName,
                    MenuType = model.MenuType,
                    MenuPrice = model.MenuPrice,
                    MenuRemarks = model.MenuRemarks
                };
                result = meninfo.SaveMenuAdd(man);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteMenu(string id)
        {
            int result = 0;
            result = meninfo.DeleteMenu(id);
            return Json(result);
        }


        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditMenu(string id)
        {
            ViewData["MenuType"] = GetMenuTypeAdd();
            MenuMaintainVM vm = new MenuMaintainVM();
            MenuMaintainInfo info = new MenuMaintainInfo();
            meninfo = info.GetMenuInfoOne(id);
            vm.MenuId = meninfo.MenuId;
            vm.MenuName = meninfo.MenuName;
            vm.MenuType = meninfo.MenuType;
            vm.MenuPrice = meninfo.MenuPrice;
            vm.MenuRemarks = meninfo.MenuRemarks;
            Mapper.DynamicMap(meninfo, vm);
            return View(vm);
        }

        /// <summary>
        /// 保存菜单（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveMenuEdit(MenuMaintainVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                MenuMaintainInfo man = new MenuMaintainInfo()
                {
                    MenuId = model.MenuId,
                    MenuName = model.MenuName,
                    MenuType = model.MenuType,
                    MenuPrice = model.MenuPrice,
                    MenuRemarks = model.MenuRemarks
                };
                result = meninfo.SaveMenuEdit(man);
            }
            return Json(result);
        }
    }
}
